<?php

lt_include(PLOG_CLASS_PATH.'class/plugin/pluginbase.class.php');

class PluginGoogleAdsense extends PluginBase
{
    var $pluginEnabled;
    var $client;
    var $contentEnabled;
    var $contentWidth;
    var $contentHeight;
    var $contentFormat;
    var $contentType;
    var $contentChannel;

    function PluginGoogleAdsense($source = "")
    {
        $this->PluginBase($source);
        $this->id = 'googleadsense';
        $this->author = 'twu2 <br /> Modified by hery@serasera.org';
        $this->desc = "This plugin offers features to support google adsense.<br />Now you don't need to modify anymore your template.";
        $this->version = '0.9.3';
        $this->locales = array('en_UK', 'zh_TW');
		if ($source == "admin") {
			$this->initAdmin();
		} else {
			$this->registerNotification( EVENT_POST_LOADED );
		}
    }

    function initAdmin()
    {
        $this->registerAdminAction('googleadsense', 'PluginGoogleAdsenseConfigAction');
        $this->registerAdminAction('updategoogleadsense', 'PluginGoogleAdsenseUpdateConfigAction');
        $menu =& Menu::getMenu();
        $this->addMenuEntry('/menu/controlCenter/manageSettings', 'GoogleAdsense', '?op=googleadsense');
    }

    function register()
	{
        $blogSettings = $this->blogInfo->getSettings();
        $this->pluginEnabled = $blogSettings->getValue('plugin_googleadsense_enabled');
        $this->client = $blogSettings->getValue('plugin_googleadsense_client');
        $this->contentWidth = $blogSettings->getValue('plugin_googleadsense_content_width');
        $this->contentHeight = $blogSettings->getValue('plugin_googleadsense_content_height');
        $this->contentFormat = $blogSettings->getValue('plugin_googleadsense_content_format');
        $this->contentType = $blogSettings->getValue('plugin_googleadsense_content_type');
        $this->contentChannel = $blogSettings->getValue('plugin_googleadsense_content_channel');

    }


	function getPluginConfigurationKeys()
	{
		return( Array(
			Array( "name" => "plugin_googleadsense_enabled", "type" => "boolean" ),
			Array( "name" => "plugin_googleadsense_client", "type" => "string" ),
			Array( "name" => "plugin_googleadsense_content_width", "type" => "integer" ),
			Array( "name" => "plugin_googleadsense_content_height", "type" => "integer" ),
			Array( "name" => "plugin_googleadsense_content_format", "type" => "string" ),
			Array( "name" => "plugin_googleadsense_content_type", "type" => "string" ),
			Array( "name" => "plugin_googleadsense_content_channel", "type" => "string" )
		));
	}


	function process( $event, $params) {

		$blogInfo = $params[ "blogInfo" ];
		$blogSettings = $blogInfo->getSettings();
		$pluginEnabled = $blogSettings->getValue( "plugin_googleadsense_enabled" );


		if (!$pluginEnabled) { return true; }

		// i HARDCODED the google script first I will put the parameters later
		if( $event == EVENT_POST_LOADED && $params["from"] != "editPost" ) {
			$post = $params[ "article" ];
			$post = $this->_googlize($post);
			$params[ "article" ] = $post;
		} 

	}

	function _googlize( $post ) {


        $blogSettings = $this->blogInfo->getSettings();

		$client = $blogSettings->getValue('plugin_googleadsense_client');
		$contentWidth = $blogSettings->getValue('plugin_googleadsense_content_width');
		$contentHeight = $blogSettings->getValue('plugin_googleadsense_content_height');
		$contentFormat = $blogSettings->getValue('plugin_googleadsense_content_format');
		$contentType = $blogSettings->getValue('plugin_googleadsense_content_type');
		$contentChannel = $blogSettings->getValue('plugin_googleadsense_content_channel');
		

		// modify the text...
		$postIntroText = $post->getIntroText();
		$postExtendedText = $post->getExtendedText();
		
		$googlescript = "
		<div class=\"googleadsense\">
		<script type=\"text/javascript\"><!--
		google_ad_client = \"" . $client . "\";
		google_ad_width = $contentWidth;
		google_ad_height = $contentHeight;
		google_ad_format = \"$contentFormat\";
		google_ad_type = \"$contentType\";
		google_ad_channel =\"$contentChannel\";
		google_page_url = document.location;
		google_color_border = \"FFFFFF\";
		google_color_link = \"014E82\";
		google_color_bg = \"FFFFFF\";
		google_color_text = \"014E82\";
		google_color_url = \"014E82\";
		//--></script>
		<script type=\"text/javascript\"
		src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
		</script>
		</div>
		";

		$post->setText( $postIntroText.POST_EXTENDED_TEXT_MODIFIER.$postExtendedText.$googlescript );
		
		/*
		// ...modify the comments...
		$comments = $post->getComments();
		
		// check if there's anything to do with the comments
		if( !is_array($comments)) {
			return $post;
		}
		
		$output = Array();
		foreach( $comments as $comment ) {
			$tmpText = $comment->getText();
			$commentText = Smileys::smileys( $this->blogInfo, $tmpText, $smileySet, $smileys );
			$comment->setText( $commentText );
			array_push( $output, $comment );
		}

		$post->setComments( $output );
		*/
		
		return $post;
	}
}

?>
